/**
 * Created by liubin on 2017/3/5.
 */



var WhmjTableEndLayer = GameCCSBase.extend({
    Panel_players : null,
    mRoundEndInfo : null,
    mHuangzhuangPeihuSeats : null,

    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/WhmjTableEnd.json");
        this.Panel_players = [];

        this.loadUI();

        this.setVisible(false);
    },

    loadUI : function(){

        //确定
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.whmjTabel.mPlayerType == 0){
                    //录像x
                    this.setVisible(false);
                    mo.whmjTabel.mWhmjReplayLayer.onPlayEnd();
                }else{
                    if (mo.whmjTabel.mwhmjJuEndedEventwhmj == null){
                        mo.gameMsgManager.sendGameReadyRequestwhmj();
                    }else{
                        //打完了 显示大结算
                        mo.whmjTabel.mwhmjJuEndedEventwhmj.setVisible(true);
                        this.setVisible(false);
                    }
                }
            }
        }, this);

        //关闭
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setVisible(false);
            }
        }, this);


        for (var i=0; i<4; i++){
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player" + i);
            this.Panel_players.push(Panel_player);
        }
    },

    setVisible : function(isVisible){
        this.unschedule(this.updateEndInfo);
        this._super(isVisible);
    },

    setEndInfo : function(roundEndInfo){

        this.mRoundEndInfo = roundEndInfo;

        //如果是黄庄要通知ui显示
        this.setHuangzhuangInfo();

        //播放胡牌音效
        this.playEndEffect(roundEndInfo);

        this.scheduleOnce(function(){
            this.updateEndInfo();
        }, 5.0);
    },

    updateEndInfo : function(){
        this.setVisible(true);

        var roundEndInfo = this.mRoundEndInfo;

        var winSeatView = -1;
        var huLogicSeat = null;
        var dianpaoLogicSeat = null;

        var resultScores = mo.whmjTableStatus.getTableResultScores();

        for (var seat=0; seat<4; seat++){
            var logicSeat = mo.whmjTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];

            var isHU = seatUser.WinCard != null && seatUser.WinCard != 0;
            if(isHU){
                huLogicSeat = logicSeat;
                dianpaoLogicSeat = seatUser.WinCardSource;
                break;
            }
        }


        for (var seat=0; seat<4; seat++){
            var logicSeat = mo.whmjTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];

            //seat
            var Text_seat = mo.seekWidgetByName(this.Panel_players[seat], "Text_seat");
            mo.gameHelper.setTextString(Text_seat, this.getSeatText(logicSeat));

            //nick
            var Text_nick = mo.seekWidgetByName(this.Panel_players[seat], "Text_nick");
            mo.gameHelper.setTextString(Text_nick, seatUser.user_info.nickname);

            //head
            var Image_head = mo.seekWidgetByName(this.Panel_players[seat], "Image_head");
            mo.gameHelper.loadImageForUrl(Image_head, seatUser.user_info.faceUrl);

            //hu
            var Image_hu = mo.seekWidgetByName(this.Panel_players[seat], "Image_hu");
            Image_hu.setVisible(true);
            if(logicSeat == huLogicSeat){
                winSeatView = seat;
                if(dianpaoLogicSeat == 0){
                    mo.gameHelper.loadTextureImage(Image_hu, "res/ccs/whmj/table/end/zimo.png");
                }else{
                    mo.gameHelper.loadTextureImage(Image_hu, "res/ccs/whmj/table/end/hu2.png");
                }
            }else if(logicSeat == dianpaoLogicSeat - 1){
                mo.gameHelper.loadTextureImage(Image_hu, "res/ccs/whmj/table/end/baohu.png");
            }else {
                Image_hu.setVisible(false);
            }

            //beishu
            var Text_multiple = mo.seekWidgetByName(this.Panel_players[seat], "Text_multiple");
            mo.gameHelper.setTextString(Text_multiple, "");

            //score
            var Text_score = mo.seekWidgetByName(this.Panel_players[seat], "Text_score");
            mo.gameHelper.setTextString(Text_score, (resultScores[seat] > 0 ? "+" : "") + resultScores[seat] + "分");

            //fanshu
            var Text_hu = mo.seekWidgetByName(this.Panel_players[seat], "Text_hu");
            mo.gameHelper.setTextString(Text_hu, "");

        }

        //播放音效
        this.playWinEffect(winSeatView);
    },

    getSeatText : function (logicSeat) {

        var seatText = null;
        var viewSeat = mo.whmjTableStatus.getViewForLogic(logicSeat, this.mRoundEndInfo.MySeat - 1);

        if (viewSeat == 0){
            seatText = "本家";
        }else if(viewSeat == 1){
            seatText = "下家";
        }else if(viewSeat == 2){
            seatText = "对家";
        }else {
            seatText = "上家";
        }
        return seatText;
    },

    playWinEffect : function(winSeat){
        if (winSeat == -1){
            //流局
            mo.soundHelper.playMjLiuju();
        }else{
            if (winSeat == 0){
                mo.soundHelper.playMjWin();
            }else{
                mo.soundHelper.playMjLose();
            }
        }
    },

    // getGangScore : function(logicSeat, runTimeList){
    //     var result = 0;
    //
    //     return result;
    // },
    //
    // getHuCardType : function(WinType){
    //     var result = "";
    //
    //     return result;
    // },
    //

    getBaoHuViewSeat : function(myLogicSeat, runTimeList){

    },

    getPeiHuViewSeat : function(myLogicSeat, runTimeList){

    },

    setHuangzhuangInfo : function(){
        var roundEndInfo = this.mRoundEndInfo;

        var isHuangzhuang = true;
        for (var seat = 0; seat < 4; seat++) {
            var logicSeat = mo.whmjTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];
            var isHU = seatUser.WinCard != null && seatUser.WinCard != 0;
            if (isHU){
                isHuangzhuang = false;
            }
        }

        if (isHuangzhuang){
            //通知ui显示
            mo.whmjTabel.mWhmjUILayer.mWhmjTableResultLayer.showHuangzhuang();

            //如果黄庄赔 找到赔胡的玩家座位号
            this.mHuangzhuangPeihuSeats = this.getPeiHuViewSeat(roundEndInfo.MySeat - 1, roundEndInfo.RunTimeResult);
        }else{
            this.mHuangzhuangPeihuSeats = [];
        }
    },


    //播放胡牌音效
    playEndEffect : function(roundEndInfo){
        for (var i = 0; i < 4; i++){
            var seatUser = roundEndInfo["SeatUser" + (i + 1)];

            //记录胡牌相关
            var isHU = seatUser.WinCard != null && seatUser.WinCard != 0;
            if (isHU){
                var isZimo = seatUser.WinCardSource == 0;
                var winType = seatUser.WinType;
                var sex = seatUser.user_info.sex;

                mo.soundHelper.playHuType(sex, isZimo, winType);
                break;
            }
        }
    }
});